package com.xlhj.boot.service.impl;

import com.xlhj.boot.service.TtlQueueProducerService;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.UUID;

/**
 * @author: liucaijing
 * @description: 延迟队列(队列延迟)
 * @date: 2021/10/20 9:02
 */
@Service
public class TtlQueueProducerServiceImpl implements TtlQueueProducerService {

    private static final String TTL_QUEUE_EXCHANGE = "ttl-queue-exchange";
    private static final String TTL_QUEUE_A_ROUT_KEY = "ttl_queue-A-routing-key";
    private static final String TTL_QUEUE_B_ROUT_KEY = "ttl_queue-B-routing-key";

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Override
    public void sendMessage(String message) {
        String uuid1 = UUID.randomUUID().toString();
        CorrelationData correlationData1 = new CorrelationData(uuid1);
        rabbitTemplate.convertAndSend(TTL_QUEUE_EXCHANGE, TTL_QUEUE_A_ROUT_KEY, "ttl消息：" + message + "为10s", correlationData1);
        String uuid2 = UUID.randomUUID().toString();
        CorrelationData correlationData2 = new CorrelationData(uuid2);
        rabbitTemplate.convertAndSend(TTL_QUEUE_EXCHANGE, TTL_QUEUE_B_ROUT_KEY, "ttl消息：" + message + "为40s", correlationData2);
    }
}
